package com.zjh.mailbox.contorller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.activerecord.tx.TxConfig;
import com.yaowk.authc.model.User;
import com.yaowk.common.kit.Constant;
import com.yaowk.common.kit.I18nKit;
import com.yaowk.common.kit.ResultKit;
import com.zjh.mailbox.model.Handle;
import com.zjh.mailbox.model.Mailbox;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zZz on 2017/5/17 0017.
 */
@TxConfig(Constant.MAILBOX_MODULE)
public class HandleController extends Controller {


    /**
     * 回复信箱
     *
     * @param content|回复内容|String|必填
     * @param handle_user|移交人|Integer|必填
     * @param mailbox_id|信箱id|Integer|必填
     * @param accessory|附件|Integer|选填
     * @title 回复信箱
     */
    @Before(Tx.class)
    @RequiresPermissions("mailbox:update")
    public void handleMailbox() {
        try {
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            Mailbox mailbox = Mailbox.dao.findById(getParaToInt("mailbox_id"));
            if(!mailbox.getHandleUser().equals(user.getId()))
            {
                renderJson(ResultKit.fail(I18nKit.get("handle_user_error", Constant.MAILBOX_MODULE, this)));
                return;
            }
            if (mailbox.getHandleStatus() == 2) {
                renderJson(ResultKit.fail(I18nKit.get("can_not_edit", Constant.MAILBOX_MODULE, this)));
                return;
            }
            Handle h = new Handle();
            h.setContent(getPara("content"))
                    .setCreateTime(new java.sql.Date(new java.util.Date().getTime()))
                    .setCreateUser(user.getId())
                    .setHandleUser(getParaToInt("handle_user"))
                    .setMailboxid(getParaToInt("mailbox_id"))
                    .setAccessory(getPara("accessory"))
                    .save();
            mailbox.setHandleUser(getParaToInt("handle_user")).update();
            renderJson(ResultKit.success(I18nKit.get("add_success", Constant.MAILBOX_MODULE, this)));
        } catch (ActiveRecordException e) {
            renderJson(ResultKit.fail(e));
        }

    }

    /**
     * 获取信箱处理详细情况
     *
     * @param mailbox_id|信箱id|Integer|必填
     * @title 获取信箱处理详细情况
     */
    @RequiresPermissions("mailbox:read")
    public void getMailboxHandle() {
        String mailboxId = getPara("mailbox_id");
        List<Mailbox> MailboxList = Mailbox.dao.getMailboxHandle(mailboxId);
        HashMap<String, Object> mailboxMap = new HashMap();
        if(MailboxList.size() == 0)
        {
            renderJson(ResultKit.fail(I18nKit.get("add_success", Constant.MAILBOX_MODULE, this)));
        }
        Mailbox mailbox = MailboxList.get(0);
        mailboxMap.put("id", mailbox.get("MailboxId"));
        mailboxMap.put("title", mailbox.get("MailboxTitle"));
        mailboxMap.put("content", mailbox.get("MailboxContent"));
        mailboxMap.put("create_user", mailbox.get("MailboxCreateUser"));
        mailboxMap.put("create_user_name", mailbox.get("MailboxCreateUserName"));
        mailboxMap.put("create_time", mailbox.get("MailboxCreateTime"));
        mailboxMap.put("accessory", mailbox.get("MailboxAccessory"));
        //List<HashMap> handleList = new ArrayList<>();
        List<Handle> handleList = Handle.dao.getMailboxHandles(mailboxId);
        mailboxMap.put("handles",handleList);
        //mailboxMap.put("handles", handleList);
        /*
        int listSize = MailboxList.size();
        for (int i = 0; i < listSize; i++) {
            Mailbox m = MailboxList.get(i);
            HashMap<String, Object> handleMap = new HashMap<>();
            handleMap.put("id", m.get("HandleId"));
            handleMap.put("content", m.get("HandleContent"));
            handleMap.put("create_user", m.get("HandleCreateUser"));
            handleMap.put("create_username", m.get("HandleCreateUserName"));
            handleMap.put("handle_user", m.get("HandleHandleUser"));
            handleMap.put("handle_user_name", m.get("HandleHandleUserName"));
            handleMap.put("accessory", m.get("HandleAccessory"));
            handleMap.put("create_time", m.get("HandleCreateTime"));
            handleList.add(handleMap);
        }
*/
        renderJson(ResultKit.success(mailboxMap));
    }

    /**
     * 屏蔽处理情况
     *
     * @param handle_id|处理过程id|Integer|必填
     * @title 屏蔽处理情况
     */
    @RequiresPermissions("mailbox:update")
    public void shieldHandle() {
        Handle h = Handle.dao.findById(getParaToInt("handle_id"));
        boolean result = h.setShield(0).update();
        if (result) {
            renderJson(ResultKit.success(I18nKit.get("edit_success", Constant.MAILBOX_MODULE, this)));
        } else {
            renderJson(ResultKit.fail(I18nKit.get("edit_fail", Constant.MAILBOX_MODULE, this)));
        }
    }

    /**
     * 取消屏蔽处理情况
     *
     * @param handle_id|处理过程id|Integer|必填
     * @title 屏蔽处理情况
     */
    @RequiresPermissions("mailbox:update")
    public void cancelShieldHandle() {
        Handle h = Handle.dao.findById(getParaToInt("handle_id"));
        boolean result = h.setShield(1).update();
        if (result) {
            renderJson(ResultKit.success(I18nKit.get("edit_success", Constant.MAILBOX_MODULE, this)));
        } else {
            renderJson(ResultKit.fail(I18nKit.get("edit_fail", Constant.MAILBOX_MODULE, this)));
        }
    }

    /**
     * 是否可取回修改
     *
     * @param handle_id|处理过程id|Integer|必填
     * @title 是否可取回修改
     */
    @RequiresPermissions("mailbox:read")
    public void canRetrieveHandle() {
        Handle h = Handle.dao.findById(getParaToInt("handle_id"));
        if (h.getRetrieve() == 0) {
            renderJson(ResultKit.success(I18nKit.get("can_not_retrieve", Constant.MAILBOX_MODULE, this)));
        } else {
            renderJson(ResultKit.fail(I18nKit.get("can_retrieve", Constant.MAILBOX_MODULE, this)));
        }
    }

    /**
     * 取回修改
     *
     * @param handle_id|处理过程id|Integer|必填
     * @param content|回复内容|String|必填
     * @param handle_user|移交人|Integer|必填
     * @param accessory|附件|String|必填
     * @title 取回修改
     */
    @RequiresPermissions("mailbox:update")
    public void editHandle() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Handle h = Handle.dao.findById(getParaToInt("handle_id"));
        boolean result = h.setContent(getPara("content"))
                .setEditTime(new java.sql.Date(new java.util.Date().getTime()))
                .setEditUser(user.getId())
                .setHandleUser(getParaToInt("handle_user"))
                .setAccessory(getPara("accessory"))
                .update();
        if (result) {
            renderJson(ResultKit.success(I18nKit.get("edit_success", Constant.MAILBOX_MODULE, this)));
        } else {
            renderJson(ResultKit.fail(I18nKit.get("edit_fail", Constant.MAILBOX_MODULE, this)));
        }
    }

}
